package com.jason.kotlin.meta.architecture2.ui

import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture2.base.WanBaseActivity
import com.jason.library.common.adapter.CommonViewPagerAdapter
import com.jason.kotlin.meta.architecture2.ui.fragments.*
import com.jason.kotlin.meta.architecture2.ui.utils.MainBottomNav

class WanAndroidActivity : WanBaseActivity() {

    private lateinit var mainPager: ViewPager
    private lateinit var bottomLayout: TabLayout


    override fun getLayoutId(): Int = R.layout.activity_wan_android

    override fun onViewInit() {
        mainPager = findViewById(R.id.vp_main_container)
        mainPager.offscreenPageLimit = 5
        bottomLayout = findViewById(R.id.bottom_nav)

        mainPager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(bottomLayout)
        )

        MainBottomNav.setBottomTabs(bottomLayout, layoutInflater)
        bottomLayout.addOnTabSelectedListener(
            object : TabLayout.ViewPagerOnTabSelectedListener(mainPager) {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    mainPager.currentItem = tab.position
                }


                override fun onTabReselected(tab: TabLayout.Tab?) {

                }
            })
    }

    override fun onDataInit() {
        val pagerAdapter =
            CommonViewPagerAdapter(
                supportFragmentManager
            )
        pagerAdapter.addFragment(HomeFragment(), getString(R.string.wan_home_title))
        pagerAdapter.addFragment(ProjectFragment(), getString(R.string.wan_project_title))
        pagerAdapter.addFragment(TreeFragment(), getString(R.string.wan_square_title))
        pagerAdapter.addFragment(PublicFragment(), getString(R.string.wan_public_title))
        pagerAdapter.addFragment(MyFragment(), getString(R.string.wan_my_title))

        mainPager.adapter = pagerAdapter

        mainPager.currentItem = 0
    }


}